﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WFONE
{

    class CDeviceList
    {
        public static CDevice cDeviceListHead = null;
        public static CDevice cDeviceListTail = null;
        public static long DeviceNum = 0;
        //清空设备库的所有数据
        public static void DeleteDevice()
        {
        }
        //删除某个设备
        public static void DeleteDevice(string DeviceName)
        {
            CDevice current = null;
            //如果设备库为空，自然就不需要记录做删除操作
            if (DeviceNum == 0)
            {
                MessageBox.Show("设备库为空");
            }
            else
            {
                current = cDeviceListHead;
                CDevice BeforeCurrent = cDeviceListHead;        //用于记录要删除的设备在链表中的前一个设备
                while (current != null && current.name != DeviceName)
                {
                    BeforeCurrent = current;
                    current = current.next;
                }
                if (current == null)
                {
                    MessageBox.Show("并没有这种设备");
                }
                else
                {

                    if (current == BeforeCurrent)
                    {
                        //刚好要删除的设备位于链表中的第一个
                        cDeviceListHead = current.next;
                    }
                    else
                    {
                        BeforeCurrent.next = current.next;
                    }
                    DeviceNum--;
                    //更新数据库数据程序段（缺）
                    /*

                    */
                    MessageBox.Show("删除设备" + BeforeCurrent.name + "成功");
                }

            }

        }
        //查询设备，获取设备指针
        public static CDevice SearchDevice(string DeviceName)
        {
            CDevice current = null;
            //如果设备库为空，自然就不需要记录做查询操作
            if (DeviceNum == 0)
            {
                MessageBox.Show("设备库为空");
            }
            else
            {
                current = cDeviceListHead;
                while (current != null && current.name != DeviceName)
                    current = current.next;
                if (current == null)
                {
                    //      MessageBox.Show("并没有这种设备");
                }

            }
            return current;
        }

        //获取设备库链表的链表头
        public static CDevice getCDeviceHead()
        {
            if (cDeviceListHead == null)
            {
                return cDeviceListHead;
            }
            else
            {
                MessageBox.Show("设备库数据为空");
                return null;
            }
        }
        //添加设备
        public static CDevice AddDevice(String DeviceName)
        {
            CDevice y = new CDevice();
            y.name = DeviceName;

            CDevice current = cDeviceListHead;

            if (cDeviceListHead == null)
            {
                cDeviceListHead = y;
                cDeviceListTail = y;
                DeviceNum = 1;
                //   MessageBox.Show("添加设备" + y.name + "成功");
                return cDeviceListHead;

            }
            else
            {
                current = SearchDevice(DeviceName);
                if (current == null)//没找到
                {
                    cDeviceListTail.next = y;
                    cDeviceListTail = y;
                    DeviceNum++;
                    //   MessageBox.Show("添加设备" + y.name + "成功");
                    current = y;
                }
                else
                {
                    //  MessageBox.Show("该设备已存在！");                    
                }
                return current;
            }

            /*
            更新数据文件的记录的程序段（缺失）
            */
        }

    }

    public class CDevice
    {
        //含设备的信息和控制设备数据的方法 （注意区分“设备信息”和“设备数据”两个概念）
        public string name;//设备名字
        public int DataNumSl = 0;//设备的单面数据的组数
        public int DataNumDl = 0;//设备的单面数据的组数
        public CDevice next = null;

        //public static 
        public CDeDataSl cDeDataSingleListHead = null; //设备单面数据链表的头指针
        public CDeDataSl cDeDataSingleListTail = null;//设备单面数据链表的尾指针
        public CDeDataDl cDeDataDoubleListHead = null; //设备双面数据链表的头指针
        public CDeDataDl cDeDataDoubleListTail = null;//设备双面数据链表的尾指针
        //清空设备的所有数据
        public void DeleteDeData()
        {
            cDeDataSingleListHead = null;
            cDeDataSingleListTail = null;
            cDeDataDoubleListHead = null;
            cDeDataDoubleListTail = null;
        }


        //删除设备的一组单面数据
        public void DeleteDeDataSl(DateTime x)
        {
            CDeDataSl current = null;
            CDeDataSl BeforeCurrent = null;
            if (cDeDataSingleListHead == null)
            {
                MessageBox.Show("该设备的数据记录为空");
            }
            else
            {
                current = cDeDataSingleListHead;
                while (current != null && current.datetime == x)
                {
                    BeforeCurrent = current;
                    current = current.next;
                }
                if (current != null)
                {
                    BeforeCurrent.next = current.next;
                    DataNumSl--;
                    MessageBox.Show("成功删除该组数据记录");
                    if (current.next == null)
                        cDeDataSingleListTail = BeforeCurrent;
                }
                else
                {
                    MessageBox.Show("该设备并无此记录");
                }

            }
        }
        //删除设备的一组双面面数据
        public void DeleteDeDataDl(DateTime x)
        {
            CDeDataDl current = null;
            CDeDataDl BeforeCurrent = null;
            if (cDeDataDoubleListHead == null)
            {
                MessageBox.Show("该设备的数据记录为空");
            }
            else
            {
                current = cDeDataDoubleListHead;
                while (current != null && current.datetime == x)
                {
                    BeforeCurrent = current;
                    current = current.next;
                }
                if (current != null)
                {
                    BeforeCurrent.next = current.next;
                    DataNumDl--;
                    MessageBox.Show("成功删除该组数据记录");
                    if (current.next == null)
                        cDeDataDoubleListTail = BeforeCurrent;
                }
                else
                {
                    MessageBox.Show("该设备并无此记录");
                }

            }
        }

        //添加一组单面设备数据
        public void AddDeData(CDeDataSl o)
        {
            CDeDataSl y = new CDeDataSl();
            y.datetime = o.datetime;
            y.dPAngle = o.dPAngle;
            y.dRp = o.dRp;
            y.dRs = o.dRs;
            y.dRx = o.dRx;
            y.dGp = o.dGp;
            y.dA1 = o.dA1;
            y.dA2 = o.dA2;
            y.next = o.next;
            if (DataNumSl >= 10)
            {
                DataNumSl--;
                cDeDataSingleListHead = cDeDataSingleListHead.next;
            }
            if (DataNumSl == 0)
            {
                cDeDataSingleListHead = cDeDataSingleListTail = y;
                DataNumSl++;
            }
            else
            {
                cDeDataSingleListTail.next = y;
                cDeDataSingleListTail = y;
                DataNumSl++;
            }
        }
        //添加一组双面面设备数据
        public void AddDeData(CDeDataDl o)
        {
            CDeDataDl y = new CDeDataDl();
            y.datetime = o.datetime;
            y.dPAngle1 = o.dPAngle1;
            y.dR1p = o.dR1p;
            y.dR1s = o.dR1s;
            y.dR1x = o.dR1x;
            y.dG1p = o.dG1p;
            y.dA11 = o.dA11;
            y.dA12 = o.dA12;

            y.dR2p = o.dR2p;
            y.dR2s = o.dR2s;
            y.dR2x = o.dR2x;
            y.dG2p = o.dG2p;
            y.dA21 = o.dA21;
            y.dA22 = o.dA22;
            y.next = o.next;
            if (DataNumDl >= 10)
            {
                DataNumDl--;
                cDeDataDoubleListHead = cDeDataDoubleListHead.next;
            }
            if (DataNumDl == 0)
            {
                cDeDataDoubleListHead = cDeDataDoubleListTail = y;
                DataNumDl++;
            }
            else
            {
                cDeDataDoubleListTail.next = y;
                cDeDataDoubleListTail = y;
                DataNumDl++;
            }
        }
        //按日期查询设备的一组数据
        public CDeDataSl SearchSl(DateTime T)
        {
            CDeDataSl current = null;
            if (DataNumSl == 0)
            {
                MessageBox.Show("该设备并无记录数据");
            }
            else
            {
                current = cDeDataSingleListHead;
                while (current != null && current.datetime != T)
                    current = current.next;
                if (current == null)
                {
                    MessageBox.Show("并没有这组记录");
                }
            }
            return current;
        }
        //按日期查询设备的一组数据
        public CDeDataDl SearchDl(DateTime T)
        {
            CDeDataDl current = null;
            if (DataNumSl == 0)
            {
                MessageBox.Show("该设备并无记录数据");
            }
            else
            {
                current = cDeDataDoubleListHead;
                while (current != null && current.datetime != T)
                    current = current.next;
                if (current == null)
                {
                    MessageBox.Show("并没有这组记录");
                }
            }
            return current;
        }

    }

    public class CDeDataSl
    {
        //设备数据
        public CDeDataSl next = null;
        public DateTime datetime = DateTime.Now;//记录日期
        public double dRs = 0;//试重块的半径，单位MM
        public double dRx = 0;//校正块的半径，单位MM
        public double dGp = 0;//单个平衡块质量。单位g
        public double dRp = 0;//平衡块半径，单位MM
        public double dPAngle = 0;//平衡块相对回转中心的夹角
        public double dA1 = 0;//平衡块A1的位置
        public double dA2 = 0;//平衡块A2的位置
                              /*public void Create()
                              {
                                  dRs = 0;
                                  dRx = 0;
                                  dGp = 0;
                                  dRp = 0;
                                  dPAngle = 0;
                              }*/

    }
    public class CDeDataDl
    {
        //设备数据
        public CDeDataDl next = null;
        public DateTime datetime = DateTime.Now;//记录日期
        public double dR1s = 0;//测试面1的试重块的半径，单位MM
        public double dR1x = 0;//测试面1的校正块的半径，单位MM
        public double dG1p = 0;//测试面1的单个平衡块质量。单位g
        public double dR1p = 0;//测试面1的平衡块半径，单位MM
        public double dPAngle1 = 0;//测试面1的平衡块相对回转中心的夹角
        public double dA11 = 0;//平衡块A11的位置
        public double dA12 = 0;//平衡块A12的位置

        public double dR2s = 0;//测试面2的试重块的半径，单位MM
        public double dR2x = 0;//测试面2的校正块的半径，单位MM
        public double dG2p = 0;//测试面2的单个平衡块质量。单位g
        public double dR2p = 0;//测试面2的平衡块半径，单位MM
        public double dPAngle2 = 0;//测试面2的平衡块相对回转中心的夹角
        public double dA21 = 0;//平衡块A21的位置
        public double dA22 = 0;//平衡块A22的位置


    }
}

